from django.http import HttpResponse
from django.shortcuts import render, redirect, reverse
from django.utils.deprecation import MiddlewareMixin


class CmsLoginRequired(MiddlewareMixin):
    def process_request(self, request):
        if request.path.startswith('/cms/'):  # 后台
            if request.user.is_authenticated:  # 已登陆
                if request.user.is_active:
                    if not (request.user.is_staff or request.user.is_superuser):
                        return render(request, 'error.html', {"msg": "您的权限不够哦~~"})
                else:
                    return render(request, 'error.html', {"msg": "您的账号已冻结哦~~"})
            else:
                return redirect(reverse("auth:login"))
